<?php
/**
 * Moduleadmin_Data_Modulelist
 * get all modules data
 * @author Adrian Stolarski
 */
class Moduleadmin_Data_Modulelist implements Moduleadmin_Data_Mainable, 
        Moduleadmin_Data_Preparable, Moduleadmin_Data_Tokenable, 
        Moduleadmin_Data_Templatable {
    /**
     *
     * @var type $database
     * database handler
     */
    public $database;
    /**
     *
     * @var type $database_query
     * database query handler
     */
    public $database_query;
    /**
     *
     * @var type $random_token
     * random token handler
     */
    public $random_token;
    /**
     * constructor()
     */
    public function __construct() {
        $this->database = new Core_Driver_MysqlImproved;
    }
    
    /**
     * main()
     * return module list
     * @param array $variables
     * @return type $page_data
     */
    public function main(array $variables = null) {
        $page = array();
        $page['csrf_token'] = $this->create_token();
        $page['template'] = $this->get_template_name();
        $this->database->connect();
        foreach($variables as $key => $value){
            $variables[$key] = $this->database->escape($value);
        }
        $this->prepare_query($variables);
        //echo $this->database_query;
        $this->database->prepare($this->database_query);
        $this->database->query();
        $page_data = array();
        while($module_data = $this->database->fetch('array')){
            $page['page_data'][$module_data['module_name']] = array(
                'module_name' => $module_data['module_name'],
                'module_title' => $module_data['module_title']
            );
        }
        //var_dump($page);
        //die();
        return $page;
    }

    public function prepare_query(array $variables = null) {
        //var_dump($variables);
        $this->database_query = "SELECT * FROM page_module;";
    }
    
    /**
     * create_token()
     * create random roken
     * @return type $token
     */
    public function create_token() {
        $this->random_token = new Core_Data_Tokenizer;
        return $this->random_token->create_token();
    }
    
    /**
     * get_template_name()
     * get current template name
     */
    public function get_template_name() {
        $template_name = SERVER_ROOT."moduleadmin/views/modulelist.php";
        return $template_name;
    }

}
